from flask import Flask, render_template, request ,url_for
import sqlite3
import random

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/index')
def home():
    return index()


@app.route('/movie')
def movie():
    datalist = []
    randomList = []
    conn = sqlite3.connect("moves.db")
    cursor = conn.cursor()
    start = 0
    pageSize = 250
    total = 250
    slq = f'select * from movies250 order by id limit {start}, {pageSize} '
    a = random.randint(0, 246)
    slq2 = f"select * from movies250 order by id  limit {a},9"
    slq3 = 'select count(*) as total from movies250'
    data = conn.execute(slq)
    data2 = conn.execute(slq2)
    # total = conn.execute(slq3)
    print(total)
    for item in data:
        datalist.append(item)
    for item in data2:
        randomList.append(item)
    cursor.close()
    conn.close()
    return render_template('movie.html', data=datalist, start=start, total=total, pageSize=pageSize, randomList=randomList)


# @app.route('/movie/<start>/<pageSize>')
# def moviePagination(start, pageSize):
#     datalist = []
#     conn = sqlite3.connect("moves.db")
#     cursor = conn.cursor()
#     start = start
#     pageSize = pageSize
#     total = 250
#     slq = f'select * from movies250 order by id limit {start}, {pageSize} '
#     data = conn.execute(slq)
#     for item in data:
#         datalist.append(item)
#     cursor.close()
#     conn.close()
#     return render_template('movie.html', data=datalist, total=total, pageSize=pageSize)
#

@app.route('/word')
def word():
    return render_template('word.html')


@app.route('/score')
def score():
    dateX = []
    dataSeries = []
    conn = sqlite3.connect("moves.db")
    cursor = conn.cursor()
    slq = '''
     select score ,count(score) from movies250 group by score
     '''
    data = conn.execute(slq)
    for item in data:
        dateX.append(str(item[0]))
        dataSeries.append((item[1]))
    cursor.close()
    conn.close()
    return render_template('score.html', dateX=dateX, dataSeries=dataSeries)


@app.route('/team')
def team():
    return render_template('team.html')


if __name__ == '__main__':
    app.run()
